/*
 *  Copyright (C) 2022  贺龙宇
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
//TODO: Optimize
#include"log.h"
Log::Log()
{
  time_t currentTime = std::time(NULL);
  char chCurrentTime[128];
  std::strftime(chCurrentTime, sizeof(chCurrentTime), "%m%d%H%M", std::localtime(&currentTime));
  std::string stCurrentTime = chCurrentTime;
  std::string str_command;
  str_command="touch "+stCurrentTime+".log";
  std::cout<<str_command<<std::endl;
  char command[128];
  memset(command,0,sizeof(command));
  String2Char(command,str_command);
  system(command);
  printf("Log file created successfully. Program data will be written to the above file\n");
  program_start_time = (int)std::clock();
  file_name = stCurrentTime+".log";
  writeLog("Program started");
  log_type[0]="Default";
  log_type[1]="Select";
  log_type[2]="TFLuna";
  log_type[3]="SerialPort";
  log_type[4]="Error";
}
void Log::Init()
{
  time_t currentTime = std::time(NULL);
  char chCurrentTime[128];
  std::strftime(chCurrentTime, sizeof(chCurrentTime), "%m%d%H%M", std::localtime(&currentTime));
  std::string stCurrentTime = chCurrentTime;
  std::string str_command;
  str_command="touch "+stCurrentTime+".log";
  std::cout<<str_command<<std::endl;
  char command[128];
  String2Char(command,str_command);
  system(command);
  printf("Log file created successfully. Program data will be written to the above file\n");
  program_start_time = std::clock();
  file_name = stCurrentTime+".log";
  writeLog("Program started");
}
Log::~Log()
{
  writeLog("Program stopped");
}

inline void Log::String2Char(char * cmd, const std::string &str)
{
  for(int i=0;i<str.length();i++)
    {
      cmd[i]=str[i];
    }
}

void Log::writeLog(const std::string &str, int log_mode)
{
  time_t currentTime = std::time(NULL);
  char chCurrentTime[128];
  std::strftime(chCurrentTime, sizeof(chCurrentTime), "%H:%M:%S", std::localtime(&currentTime));
  std::string stCurrentTime = chCurrentTime;
  // int program_time = ((int)std::clock()-program_start_time)/1000;
  std::string str_command = "echo '" + stCurrentTime + " # " + log_type[log_mode] +
                         +" # " + str + "'>>" + file_name;
//  std::cout<<str_command;
  char command[256];
  memset(command,0,sizeof(command));
  String2Char(command,str_command);
  system(command);
}

std::string Float2String(const float &f, int presision)
{
  std::string s;
  int inter = (int)f;
  double decimal = f-inter;
  for(int i=1;i<=presision;i++)
    {
      decimal*=10.0;
    }
  std::string dec = std::to_string((int)decimal);
  if(dec.length()==presision)
  {
      s=std::to_string(inter)+"."+dec;
  }
  else
    {
      s=std::to_string(inter)+".0"+dec;
    }
  return s;
}



Log *FYTLog = new Log;

